import Sortable from 'sortablejs'
/**
 * @desc el-table 排序
 * v-sort-table="list" 全部可拖动
 * modifiers 第一个参数为可拖动列的class 默认为全部可拖动
 * 必须设置 :row-key="XXX"
 */
export const sort_table = {
    update(el, binding, vnode) {
        el.classList.add('sort_table')
        const list = binding.value
        const moveTarget = Object.keys(binding.modifiers)[0]
        const tbody = el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
        list.length && Sortable.create(tbody, {
            ghostClass: '_sortable',
            handle: moveTarget ? '.' + moveTarget : null,
            setData: function(dataTransfer, dragEl) {
                dataTransfer.setData('Text', dragEl.textContent) // 兼容火狐
            },
            onEnd: evt => {
                list.splice(evt.newIndex, 0, list.splice(evt.oldIndex, 1)[0])
                vnode.componentInstance.$nextTick(() => {
                    formBlur(vnode)
                })
            }
        })
    }
}
// 列表中form表单失焦
function formBlur(table){
    const tableCol = table.componentInstance.$children.at(-1).$children
    tableCol.forEach(item => item.$children.forEach(f => f.blur && f.blur()))
}